from flask import current_app, g
from flask import session
import functools


def do_index_class(index):
    """对传入的index下标传入不同放入class属性值"""
    if index == 1:
        return "first"
    elif index == 2:
        return "second"
    elif index == 3:
        return "third"
    else:
        return ""


def login_user_data(view_func):
    # 防止内层装饰器修改被装饰的函数的名字
    @functools.wraps(view_func)
    def wrapper(*args,**kwargs):
        user_id = session.get("user_id")
        user = None
        # 2.根据用户id查询所有数据
        try:
            # 延迟导入
            from info.models import User
            if user_id:
                user = User.query.get(user_id)
        except Exception as e:
            current_app.logger.error(e)

        # 保存数据到View_func函数中能够获取到用户数据
        g.user = user
        result = view_func(*args,**kwargs)
        return result

    return wrapper

